var common = require('./utils/common');
//app.js
App({
  globalData:{
    longitude:360,
    latitude:360
  },
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    let that = this;
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        that.globalData.longitude = res.longitude;
        that.globalData.latitude = res.latitude;
        common.asynPost(common.API.user.setLocation,{
          latitude:res.latitude,
          longitude:res.longitude
        },function(data){
          console.log(data["header"].statusCode)
        })
      }
    })
  },
  getUserInfo: function (cb) {
    var that = this
    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      typeof cb == "function" && cb(userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function (sul) {
          if (sul && sul.code) {
            that.login(sul.code, cb);
          }
        }
      })
    }
  },
  login: function (code, cb) {
    let that = this;
    wx.getUserInfo({
      withCredentials: true,
      success: function (res) {
        common.asynPost(common.API.user.login, {
          code: code,
          encryptedData: res.encryptedData,
          signature: res.signature,
          iv: res.iv
        }, function (data) {
          if (data['userInfo']) {
            wx.setStorage({
              key: 'userInfo',
              data: data['userInfo'],
            })
            typeof cb == "function" && cb(data['userInfo']);
          }
        });
      }
    });
  }
})